import { systemDictAll } from "@/api/dictionary";
import { generateKVMap, generateOptionsMap, generateFormOptionsMap } from "@/util";

const state = {
  DICT: [],
  KV_MAP: {},
  OPTION_MAP: {},
  FORM_OPTION_MAP: {},
};

const getters = {};

const mutations = {
  SET_DICT: (state, data) => {
    state.DICT = data;
  },
  SET_KV_MAP: (state, data) => {
    state.KV_MAP = data;
  },
  SET_OPTION_MAP: (state, data) => {
    state.OPTION_MAP = data;
  },
  SET_FORM_OPTION_MAP: (state, data) => {
    state.FORM_OPTION_MAP = data;
  },
};

const actions = {
  getDictAll({ commit }) {
    return new Promise((resolve, reject) => {
      systemDictAll()
        .then((response) => {
          commit("SET_DICT", response);
          commit("SET_KV_MAP", generateKVMap(response));
          commit("SET_OPTION_MAP", generateOptionsMap(response));
          commit("SET_FORM_OPTION_MAP", generateFormOptionsMap(response));
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
